package com.lagou.edu.controller;

import com.lagou.edu.model.entity.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @program: stage_1_module_3
 * @description:
 * @author: zhangxiaoyang
 * @date: 2020-07-18 21:07
 **/
@Controller
@RequestMapping("/resumes")
public class ResumeController {


    @Autowired
    private ResumeService resumeService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView listResume(ModelAndView modelAndView) {
        System.out.println(">>>>>>>>>>>>跳转到列表页");
        List<Resume> list = resumeService.list();
        modelAndView.addObject("data", list);
        modelAndView.setViewName("list");
        return modelAndView;
    }

    @RequestMapping(value = "/toAdd", method = RequestMethod.GET)
    public String toAdd() {
        return "add";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Resume resume) {
        resumeService.save(resume);
        return "redirect:/resumes/list";
    }

    @RequestMapping(value = "/toEdit", method = RequestMethod.GET)
    public String toEdit(Long id, Model model) {
        Resume resume = resumeService.get(id);
        model.addAttribute("data", resume);
        return "edit";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public String delete(Long id, Model model) {
        resumeService.delete(id);
        return "redirect:/resumes/list";
    }

}
